package smlab.view.ui.toolTip
{
	
	import flash.accessibility.AccessibilityProperties;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.text.*;
	
	import smlab.utils.DisplayObjectUtil;
	import smlab.utils.PopUpUtils;
	
	/**
	 * 提示类
	 * Simon
	 * 
	 * 原作者信息
	 * @link kinglong@gmail.com
	 * @author Kinglong
	 * @version 0.1
	 * @since 20090608
	 * @playerversion fp9+
	 */	
	public class ToolTip_SM extends Sprite
	{
		private static var _instance:ToolTip_SM = null;
		private static var _backColor:uint;
		private static var _txtColor:uint;
		private static var _fontSize:uint;
		private static var _font:String;
		private static var _isShowArrow:Boolean=true;
		private static var _backAlpha:Number = 1;
		private static var _showShadow:Boolean = false;
		private static var _shadowColor:uint = 0x000000;
		private static var _shadowAlpha:Number = 0.4;
		private var _label:TextField;		
		private var _area:DisplayObject;
		
		public function ToolTip_SM()
		{
			_label = new TextField();
			_label.autoSize = TextFieldAutoSize.LEFT;
			_label.selectable = false;
			_label.multiline = false;
			_label.wordWrap = false;
			_label.defaultTextFormat = new TextFormat(_font, _fontSize, _backColor);
			_label.text = "提示信息";
			_label.textColor=_txtColor;
			_label.x = 5;
			_label.y = 2;
			addChild(_label);
			redraw();
			visible = false;
			mouseEnabled = mouseChildren = false;
		}
		
		//绘制 Tips 背景
		private function redraw():void
		{
			var w:Number = 10 + _label.width;
			var h:Number = 4 + _label.height;			
			this.graphics.clear();
			
			//绘制背景
			this.graphics.beginFill(_backColor,_backAlpha);
			this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
			
			if(_isShowArrow){
				this.graphics.moveTo(3, h);
				this.graphics.lineTo(9, h);
				this.graphics.lineTo(6, 5 + h);
				this.graphics.lineTo(3, h);
				this.graphics.endFill();
			}
			
			if(_showShadow){
				//绘制投影
				this.graphics.beginFill(_shadowColor, _shadowAlpha);
				this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
				
				if(_isShowArrow){
					this.graphics.moveTo(6, 3 + h);
					this.graphics.lineTo(12, 3 + h);
					this.graphics.lineTo(9, 8 + h);
					this.graphics.lineTo(6, 3 + h);
					this.graphics.endFill();
				}
			}
		}
		
		/**
		 * 实例化 ToolTip_SM 并把它 addChild 到舞台（或者其它显示容器）
		 * 
		 * @param baseContainer	tips 的容器
		 * @param backColor		背景颜色
		 * @param txtColor		文字颜色
		 * @param fontSize		字体大小
		 * @param font			字体（请使用系统默认字体或者你已经明确嵌入到swf中的字体）
		 * @param isShowArrow	是否显示小箭头(指针)
		 * @param backAlpha		背景透明度
		 * @param showShadow	是否显示阴影
		 * @param shadowColor	阴影颜色
		 * @param shadowAlpha	阴影透明度
		 * 
		 */
		public static function init(baseContainer:DisplayObjectContainer,
									backColor:uint=0x666666,
									txtColor:uint=0,
									fontSize:uint=12,
									font:String="宋体",
									isShowArrow:Boolean=true,
									backAlpha:Number = 1,
									showShadow:Boolean = false,
									shadowColor:uint = 0x000000,
									shadowAlpha:Number = 1
									):void
		{
			if (_instance == null)
			{
				_backColor=backColor;
				_txtColor=txtColor;
				_fontSize=fontSize;
				_font=font;
				_isShowArrow=isShowArrow;
				_backAlpha = backAlpha;
				_showShadow = showShadow;
				_shadowColor = shadowColor;
				_shadowAlpha = shadowAlpha;
				_instance = new ToolTip_SM();
			}
			
			baseContainer.addChild(_instance);			
		}
		
		/**
		 * 注册一个被提示对象
		 * @param _area
		 * @param message
		 * 
		 */
		public static function register($interActiveObject:DisplayObject, msg:String=""):void
		{
			if(_instance != null){
				var prop:AccessibilityProperties = new AccessibilityProperties();
				prop.description = msg;
				$interActiveObject.accessibilityProperties = prop;
				$interActiveObject.addEventListener(MouseEvent.MOUSE_OVER, _instance.handler);
			}
		}
		
		/**
		 * 注销一个被提示对象
		 * @param _area
		 * 
		 */
		public static function unregister($interActiveObject:DisplayObject):void
		{
			if (_instance != null) {
				$interActiveObject.removeEventListener(MouseEvent.MOUSE_OVER, _instance.handler);
			}
		}
		
		public function show(area:DisplayObject):void
		{
			this._area = area;
			this._area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
			this._area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
			_label.text = _area.accessibilityProperties.description;
			_label.textColor=_txtColor;
			_label.background = false;
			//_label.backgroundColor = _backColor;
			
			var format:TextFormat = new TextFormat();
			format.font = _font;
			format.color = _txtColor;
			format.size = _fontSize;
			_label.defaultTextFormat = format;
			
			redraw();
			PopUpUtils.bringToTop(_instance);
		}
		
		public function hide():void
		{
			this._area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
			this._area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
			this._area = null;
			visible = false;
		}
		
		public function move(point:Point):void
		{			 
			var lp:Point = this.parent.globalToLocal(point);
			this.x = lp.x - 6;			
			this.y = lp.y - _label.height - 12;
			
			if(this.x<0)this.x=0;
			if(this.y<0)this.y=0;
			if(this.x+this.width>this.stage.stageWidth)this.x=this.stage.stageWidth-this.width;
			if(this.y+this.height>this.stage.stageHeight)this.y=this.stage.stageHeight-this.height;
			
			if(!visible){
				visible = true;
			}
		}
		
		private function handler(e:MouseEvent):void
		{
			switch(e.type) {
				case MouseEvent.MOUSE_OUT:
					this.hide();
					break;
				case MouseEvent.MOUSE_MOVE:
					this.move(new Point(e.stageX, e.stageY));					
					break;
				case MouseEvent.MOUSE_OVER:
					this.show(e.currentTarget as DisplayObject);
					this.move(new Point(e.stageX, e.stageY))
					break;
			}
		}
	}
}

/*

使用实例

package
{
	import flash.display.Sprite;
	
	import smlab.view.ui.toolTip.ToolTip_SM;
	
	[SWF(width=500,height=500,frameRate=30,backgroundColor=0xccddee)]
	public class D extends Sprite
	{
		public function D()
		{
			var s:Sprite=new Sprite
			s.graphics.beginFill(0xffffff);
			s.graphics.drawCircle(0,0,80);
			s.x=200;
			s.y=200;
			addChild(s);
			ToolTip_SM.init(stage,6710886,0xaaddee,44,"黑体",true);
			ToolTip_SM.register(s,"haha");
			//如果不想继续显示提示请用  ToolTip_SM.unregister(s);
		}
	}
}
*/